//
//  TravelAreaDetailsViewController.m
//  ToWhere
//
//  Created by dllo on 15/11/16.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "TravelAreaDetailsViewController.h"
#import "TraveAreaModel.h"
#import "DataAnalysis.h"
#import "TravelAreaDetailsTableViewCell.h"
#import "TravelAreaStrokeViewController.h"
#import "TravelAreaTourViewController.h"
#import "TravelAreaTopicViewController.h"

@interface TravelAreaDetailsViewController ()<UITableViewDataSource, UITableViewDelegate, TravelAreaDetailTableViewcellDelegate>



//tableView
@property  (nonatomic, retain) UITableView *tableView;
//存取数据的数组
@property (nonatomic, retain) NSMutableArray *loadArr;

@property (nonatomic, retain) UIView *titleView;

@property (nonatomic, retain) UILabel *titleLabel;

@property(nonatomic, assign) NSInteger row;

@end

@implementation TravelAreaDetailsViewController

-(void)dealloc
{
    [_tableView release];
    [_loadArr release];
    [_idNumber release];
    [_titleView release];
    [_titleLabel release];
    
    [super dealloc];
}



- (void)viewDidLoad {
    
    self.loadArr = [NSMutableArray array];
    
    
    [super viewDidLoad];
    
    
    [self loading];
    [self createTableView];
    [self createTitle];
    
    // Do any additional setup after loading the view.
}
#pragma mark 创建标题
-(void)createTitle
{
    self.view.backgroundColor = [UIColor purpleColor];
    
    self.titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64 * kHeight)];
    self.titleView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.titleView];
    [self.titleView release];
    [_titleView release];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(15 * kWidth, 25 * kHeight , 20 * kWidth , 20 * kHeight);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"Food_back"] forState:UIControlStateNormal];
    backBtn.backgroundColor = [UIColor blackColor];
    [self.titleView addSubview:backBtn];
    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
   self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 120 * kWidth, self.titleView.frame.size.height / 2 - 10 * kHeight, 240 * kWidth,  30 * kHeight)];
    self.titleLabel.font = [UIFont systemFontOfSize:17 * kWidth];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
   
    [self.titleView addSubview:self.titleLabel];
    [_titleLabel release];
}
-(void)backAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];    
}

#pragma mark 创建tableView
-(void)createTableView
{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 49 * kHeight + (1 - kWidth) * 30, self.view.frame.size.width, self.view.frame.size.height - 49 * kHeight) style:UITableViewStylePlain];
    
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[[UIView alloc]initWithFrame:CGRectZero] autorelease];
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    [self.tableView registerClass:[TravelAreaDetailsTableViewCell class] forCellReuseIdentifier:@"TravelAreaDetailsTableViewCell"];
    
    
    
}

#pragma mark 网络请求
-(void)loading
{
    
    [DataAnalysis dataAnalysisGETWithUrlString:[NSString stringWithFormat:@"http://chanyouji.com/api/destinations/%@.json?page=1", self.idNumber] parameters:nil showHuD:YES hiddenView:nil successfulBlock:^(id responseObject) {
               NSArray *arr = responseObject;
        
        
        
        for (NSDictionary *dic in arr) {
            
            TraveAreaModel *model = [TraveAreaModel travelAreaModelinitWithNSDictionary:dic];
            [self.loadArr addObject:model];
        }
        
        [self.tableView reloadData];
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@", error);
    }];
    
    
    
    
    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.loadArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300 *kHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    TravelAreaDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TravelAreaDetailsTableViewCell" forIndexPath:indexPath];
    cell.delegate = self;
    self.titleLabel.text = [NSString stringWithFormat:@"%@攻略",[self.loadArr[0] name_zh_cn]];
    cell.model = self.loadArr[indexPath.row];
    
    cell.viewOne.btn.tag = 10000 + (NSInteger)[self.loadArr[indexPath. row] ID];
       cell.viewTwo.btn.tag = 100000 + (NSInteger)[self.loadArr[indexPath.row] ID];
    
    cell.viewThree.btn.tag = 1000000 +(NSInteger)[self.loadArr [indexPath.row] ID];
   cell.selectionStyle =UITableViewCellSelectionStyleNone;
    return cell;
    
}



-(void)sendToRootViewController:(NSString *)title ID:(NSNumber *)ID tag:(NSInteger)tag
{
    if (tag == 7777) {
        TravelAreaStrokeViewController *view = [[TravelAreaStrokeViewController alloc]init];
        
        view.idNumber = ID;
        view.strTitle = title;
        [self setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:view animated:YES];

    }else if (tag == 8888){
    
    TravelAreaTourViewController *view = [[TravelAreaTourViewController alloc]init];
    view.strTitle = title;
   view.idNumber = ID;
    [self setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:view animated:YES];
        
    }else if (tag == 9999){
        
        TravelAreaTopicViewController *topic = [[TravelAreaTopicViewController alloc]init];

        topic.idNumber = ID;
        topic.strTitle = title;
        [self setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:topic animated:YES];

        
    }
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
